package com.roger.springGreenwich.gateway.controller;

import com.roger.springGreenwich.gateway.domain.Users;
import com.roger.springGreenwich.gateway.domain.UsersVo;
import com.roger.springGreenwich.gateway.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UsersMapper usersMapper;

    @GetMapping("/findById/{id}")
    public Users findById(@PathVariable Long id){
        return usersMapper.selectById(id);
    }

    @GetMapping("/add")
    public String add(){
        Users users = new Users();
        users.setId(2L);
        users.setUserName("zhangsan");
        users.setPassword("345");
        users.setGmtCreate(new Date());
        usersMapper.insert(users);

        return "success";
    }

    @GetMapping("/update/{id}")
    public String del(@PathVariable Long id){
        Users users = new Users();
        users.setId(id);
        users.setUserName("zhangsan2");
        users.setGmtModified(new Date());

        usersMapper.updateById(users);

        return "success";
    }

    @GetMapping("/findAll")
    public List<UsersVo> findAll(){
        return usersMapper.findAll();
    }
}
